import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import 'amfe-flexible/index.js'
import Vant from 'vant';
import 'vant/lib/index.css';
import moment from 'moment'
import VueClipboard from 'vue-clipboard2'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
Vue.use(VueClipboard)
Vue.use(Vant);
Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.config.ignoredElements = ['wx-open-launch-weapp', 'wx-open-subscribe']
import wx from 'weixin-js-sdk'
Vue.prototype.$wx = wx
//自定义指令
import Directives from './utils/directive';
Vue.use(Directives);
import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer)
Viewer.setDefaults({
  Options: { 'inline': true, 'button': true, 'navbar': true, 'title': true, 'toolbar': true, 'tooltip': true, 'movable': true, 'zoomable': true, 'rotatable': true, 'scalable': true, 'transition': true, 'fullscreen': true, 'keyboard': true, 'url': 'data-source' }
})
Vue.filter('moment', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
});
import SlideVerify from 'vue-monoplasty-slide-verify';
Vue.use(SlideVerify);
//请求
axios.interceptors.request.use(
  config => {
    console.log(router.history.current.path);
    let token = window.localStorage.getItem("xfq_token");
    if (token && config.url !='http://kww.zsfqlm.com/api/va/cooperationrecharge/bankcardsubmit') {
      config.headers.token = `${token}`;
    }
    if(!token && config.url !='/api/register/sendcode' && config.url !='/api/registered' && config.url !='/api/resetpaw'){
      router.push('/Login');
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
//响应
axios.interceptors.response.use(
  res => {
    if (res.data.Status == 401) {
      window.location.href = window.location.origin + '/#/Top_Lo_Re';
    } else if(res.data.Status == 302){
      window.location.href = window.location.origin + '/#/white';
    }else{
      return res
    }
  },
  err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          this.$toast('请求错误')
          break;
        case 401:
          window.location.href = err.response.data.url
          this.$toast('未授权，请重新登录')
          break;
        case 403:
          this.$toast('拒绝访问')
          break;
        case 404:
          this.$toast('请求错误,未找到该资源')
          break;
        case 405:
          this.$toast('请求方法未允许')
          break;
        case 408:
          this.$toast('请求超时')
          break;
        case 500:
          this.$toast('服务器端出错')
          break;
        case 501:
          this.$toast('网络未实现')
          break;
        case 502:
          this.$toast('网络错误')
          break;
        case 503:
          this.$toast('服务不可用')
          break;
        case 504:
          this.$toast('网络超时')
          break;
        case 505:
          this.$toast('http版本不支持该请求')
          break;
        default:
          this.$toast(`连接错误${err.response.status}`)
      }
    } else {
      this.$toast('连接到服务器失败')
    }
    return Promise.resolve(err.response)
  }
);
router.afterEach((to, from, next) => {
  console.log(to.path);
  if(window.localStorage.getItem('xfq_token') && to.path == '/'){
           router.push('/home')
   }
  if(window.localStorage.getItem('xfq_token') == null && to.path == '/'){
    router.push('/Top_Lo_Re')
  }
});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {
    App
  },
  template: '<App/>'
})
